#include "ThreadPool/LogMessage.hpp"

#include <iostream>
#include <string>
#include <arpa/inet.h>
#include <sys/io.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>


void usage(const char* arg) {
  std::cout << "usage:\n" << arg << "IP Port" << std::endl;
  exit(1);
}
int main(int argc, char* argv[]) {
  if(argc != 3) {
    usage(argv[0]);
  }

  int sock = socket(AF_INET, SOCK_STREAM, 0);

  sockaddr_in server;
  server.sin_family = AF_INET;
  server.sin_addr.s_addr = inet_addr(argv[1]);
  server.sin_port = htons(atoi(argv[2]));

  if(connect(sock, reinterpret_cast<sockaddr*>(&server), sizeof(server)) < 0) {
    LogMessage(FATAL, _F, _L, "client connect fail");
    exit(2);
  }

  char buffer[1024];
  while(1) {
    memset(buffer, 0, sizeof buffer); 
    std::cout << "请输入你要发送的信息::";
    std::cin.getline(buffer, 1024);
    if(strcmp(buffer, "quit") == 0) {
      std::cout << "clinet quit" << std::endl;
      break;
    }

    send(sock, buffer, strlen(buffer), 0);
  
    recv(sock, (void*)buffer, sizeof buffer, 0);
    std::cout << "server echo#" << buffer << std::endl;
  } 

  close(sock);

  return 0;
}
